# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from MaoMiAV3.items import UrbanPassionItem


# 情色小说-都市激情
class UrbanpassionSpider(scrapy.Spider):
    name = 'UrbanPassion'
    custom_settings = {
        'ITEM_PIPELINES': {
            "MaoMiAV3.pipelines.UrbanPassionPipeline": 310
        }
    }
    allowed_domains = ['www.3459d5328c57ace2.pw']
    start_urls = ['https://www.3459d5328c57ace2.pw/xiaoshuo/list-另类小说.html']

    def __init__(self):
        self.website_url = "https://www.3459d5328c57ace2.pw"
        self.settings_url = "https://www.3459d5328c57ace2.pw/xiaoshuo/list-另类小说-{}.html"

    # 爬取子页面
    def parse_detail(self, response):
        # 接收数据
        book_dict = response.meta["book_dict"]
        # 创建管道
        item = UrbanPassionItem()
        item["title"] = book_dict["title"]
        book_content_list = response.xpath("//div[@class='content']//p")
        content_list = []
        for i in book_content_list:
            content = i.xpath("./span/text()")
            if not len(content) == 0:
                content = content[0].extract()
                content_list.append(content)
        item["content"] = content_list
        yield item

    def parse(self, response):
        book_url_list = response.xpath("//div[@class='text-list-html']//li")
        for i in book_url_list:
            book_dict = {"title": str(i.xpath("./a/@title").extract()[0]).strip()}
            # 提取小说链接
            url = i.xpath("./a/@href").extract()[0]
            # 拼接链接
            book_dict["url"] = self.website_url + url
            yield Request(url=book_dict["url"], callback=self.parse_detail, meta={"book_dict": book_dict})
        # 获取页数
        page_list = response.xpath("//div[@class='pagination']/a[last()-2]/text()").extract()[0]
        page_list = str(page_list).split("/")
        # 当前页数
        current_page = int(page_list[0])
        # 最后页数
        last_page = int(page_list[-1])
        # 判断当前页数是否小于或等于最后一页
        print("当前页数:", current_page)
        print("总页数:", last_page)
        if current_page < last_page:
            # 下一页
            next_page = current_page + 1
            next_page = self.settings_url.format(next_page)
            yield Request(url=next_page, callback=self.parse)
